package com.normalsys.live.controller;

import com.alibaba.fastjson.JSONObject;
import com.normalsys.live.base.config.Constant;
import com.normalsys.live.base.config.ResponseCode;
import com.normalsys.live.base.exp.BizException;
import com.normalsys.live.base.util.HttpReqUtil;
import com.normalsys.live.base.util.string.ObjectTranslate;
import com.normalsys.live.base.util.string.StringUtil;
import com.normalsys.live.base.util.token.TokenUtil;
import com.normalsys.live.bean.LoginParamBean;
import com.normalsys.live.bean.UserParams;
import com.normalsys.live.service.LoginService;
import com.normalsys.live.vo.LoginVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * User: 刘天宇
 * Date: 2018/7/16
 * Time: 21:21
 * Description:
 */
@RestController
@Slf4j
@RequestMapping("/login")
public class LoginController {

    @Autowired
    private LoginService loginService;

    /**
     * 微信登录
     * @param params
     * @return
     */
    @PostMapping("/wechat")
    public LoginVO wechatSign(@RequestBody UserParams params) {
        if(StringUtil.isEmpty(params.getCode())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "code错误");
        }
        //向微信发送请求，返回json数据
        String msg = HttpReqUtil.sendGet(Constant.WX_LOGIN_URL, "appid="+ Constant.MINIAPPID + "&secret=" + Constant.MINISECRET + "&js_code=" + params.getCode() + "&grant_type=authorization_code");
        if(StringUtil.isEmpty(msg)) {//没有结果返回
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "code错误");
        }

        //将微信的json数据转成map
        Map<String,Object> map = JSONObject.parseObject(msg);

        //微信返回的错误信息
        if(map.containsKey("errcode")) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "微信请求异常");
        }
        String openid = ObjectTranslate.getString(map.get("openid"));
        return loginService.wechatSign(openid, map);
    }

    /**
     * 拉取用户信息
     * @param req
     * @param params 包括敏感数据在内的完整用户信息的加密数据
     * @return
     */
    @RequestMapping(value = "/auth", method = RequestMethod.POST)
    public LoginVO auth(HttpServletRequest req, @RequestBody UserParams params) {
        //校验session是否失效
        if(TokenUtil.isOverDue(req)) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户登录信息失效");
        }
        String userId = TokenUtil.getUserid(req);

        return loginService.getUserInfo(params.getEncryptedData(),  params.getIv(), userId);
    }


    /**
     * 发送验证码
     * @param loginParamBean
     * @return
     */
    @PostMapping("/code/send")
    public void sendCode(@RequestBody LoginParamBean loginParamBean) {
        if(StringUtil.isEmpty(loginParamBean.getPhone())) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "手机号不能为空");
        }

        loginService.sendCode(loginParamBean.getPhone());
    }



}
